class User

    def add_user(attributes)
        login = attributes[:login]
        #利用fetch
      
        login = attributes.fetch(:login)
        unless login
            raise AugumentError,'Login must be supplied'
        end
        password = attributes[:password]

        #fetch传代码块
        password = attributes.fetch(:password) {|key| raise KeyError, "Password (or false) must be supplied"}
        
        unless password
            raise ArgumentError,'Password (or false) must be supplied'
        end

        command = %w[useradd]
        if attributes[:home]
            command << '--home' << attributes
        end
        if attributes[:shell]
            command << '--shell' << attributes['shell']
        end

        if password == false
            command << '--disabled-login'
        else
            command << ' --password' << password
        end

        command << login

        if attributes[:dry_run]
            puts command.join("")
        else
            system *command
        end
    end
end

user = User.new
u = {login:'bob',password:'zzz',dry_run:true}
{}.fetch(:nosuch) {|key| puts "No key #{key}"}
user.add_user(u)